package agendamento;

import hibernate.dao.HibernateConnection;
import hibernate.entity.Servico;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import skin.LookAndFeelSelector;
import utilitarios.JPanelPersonalizavel;

import com.toedter.calendar.JDateChooser;

import java.awt.Font;
import java.awt.SystemColor;

public class FrmAltExcAgendamento extends JFrame {

	private JPanel contentPane;
	private JLabel lblCliente;
	private JLabel lblData;
	private JLabel lblFuncionario;
	private JComboBox cbListaClientes;
	private JDateChooser dtchDataAgendamento;
	private JComboBox cbFuncionario;
	private JButton btnSearch;

	private JSeparator separator;

	private JTabbedPane tabbedAgendamento;

	private JPanel panelResultAgendamento;
	private JScrollPane scrollPaneAgendamento;
	private JTable tbAgendamento;

	private JPanel panelAlterar;
	private JLabel lblDataAlt;
	private JDateChooser dtDataAlt;
	private JLabel lblHorarioAlt;
	private JComboBox cbHorarioAlt;
	private JLabel lblServicoAlt;
	private JButton btnServicoAlt;
	private JTextField txtServicoAlt;
	private JLabel lblFuncionarioAlt;
	private JComboBox cbFuncionarioAlt;
	private JLabel lblClienteAlt;
	private JComboBox cbClienteAlt;
	private JButton btnSalvarAlt;
	private JButton btnCancelarAlt;

	private JButton btnAlterar;
	private JButton btnCancelar;
	private JButton btnExcluir;

	private Servico serv;
	private CtrlAgendamento ctrl = new CtrlAgendamento();

	public FrmAltExcAgendamento() {
		setIconImage(Toolkit
				.getDefaultToolkit()
				.getImage(
						"src/Repositorio/icon.png"));
		setTitle("Procurar Agendamento");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 636, 613);
		setLocationRelativeTo(null);
		contentPane = new JPanelPersonalizavel("src/Repositorio/pink_waves.png");
		contentPane.setBackground(SystemColor.controlHighlight);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblCliente = new JLabel("Cliente: ");
		lblCliente.setFont(new Font("Arial", Font.PLAIN, 18));
		lblCliente.setBounds(30, 35, 68, 14);
		contentPane.add(lblCliente);

		cbListaClientes = new JComboBox();
		cbListaClientes.setBounds(146, 29, 435, 28);
		contentPane.add(cbListaClientes);

		lblFuncionario = new JLabel("Funcion\u00E1rio:");
		lblFuncionario.setFont(new Font("Arial", Font.PLAIN, 18));
		lblFuncionario.setBounds(30, 80, 120, 28);
		contentPane.add(lblFuncionario);

		cbFuncionario = new JComboBox();
		cbFuncionario.setBounds(146, 81, 435, 28);
		contentPane.add(cbFuncionario);

		lblData = new JLabel("Data: ");
		lblData.setFont(new Font("Arial", Font.PLAIN, 18));
		lblData.setBounds(30, 135, 92, 28);
		contentPane.add(lblData);

		dtchDataAgendamento = new JDateChooser();
		dtchDataAgendamento.setBounds(146, 135, 190, 28);
		contentPane.add(dtchDataAgendamento);

		btnSearch = new JButton("");
		btnSearch.setIcon(new ImageIcon(FrmAltExcAgendamento.class
				.getResource("/Repositorio/btn_search.png")));
		btnSearch.setBounds(518, 125, 63, 38);
		contentPane.add(btnSearch);
		btnSearch.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ctrl.preencheTabelaSearch(tbAgendamento, dtchDataAgendamento, cbListaClientes, cbFuncionario);
				
			}
		});
		
		

		separator = new JSeparator();
		separator.setBounds(10, 178, 589, 2);
		contentPane.add(separator);

		tabbedAgendamento = new JTabbedPane(JTabbedPane.TOP);
		tabbedAgendamento.setBounds(0, 192, 619, 289);
		contentPane.add(tabbedAgendamento);
		
		panelResultAgendamento = new JPanel();
		tabbedAgendamento.addTab("Agendamentos", null, panelResultAgendamento,
						null);
		panelResultAgendamento.setLayout(null);
		
		
		tbAgendamento = new JTable();

		scrollPaneAgendamento = new JScrollPane();
		scrollPaneAgendamento.setBounds(6, 5, 607, 248);
		scrollPaneAgendamento.setViewportView(tbAgendamento);	
		panelResultAgendamento.add(scrollPaneAgendamento);
		
		
		panelAlterar = new JPanel();
		tabbedAgendamento.addTab("Alterar Agendamento", null, panelAlterar,
				null);
		panelAlterar.setLayout(null);

		tabbedAgendamento.setEnabledAt(1, false);
		
		lblDataAlt = new JLabel("Data:");
		lblDataAlt.setFont(new Font("Arial", Font.PLAIN, 16));
		lblDataAlt.setBounds(10, 22, 46, 14);
		panelAlterar.add(lblDataAlt);

		dtDataAlt = new JDateChooser();
		dtDataAlt.setBounds(118, 17, 172, 28);
		panelAlterar.add(dtDataAlt);

		lblHorarioAlt = new JLabel("Hor\u00E1rio: ");
		lblHorarioAlt.setFont(new Font("Arial", Font.PLAIN, 16));
		lblHorarioAlt.setBounds(335, 22, 68, 14);
		panelAlterar.add(lblHorarioAlt);

		cbHorarioAlt = new JComboBox();
		cbHorarioAlt.setBounds(428, 16, 89, 28);
		panelAlterar.add(cbHorarioAlt);

		lblServicoAlt = new JLabel("Servi\u00E7o: ");
		lblServicoAlt.setFont(new Font("Arial", Font.PLAIN, 16));
		lblServicoAlt.setBounds(10, 66, 68, 14);
		panelAlterar.add(lblServicoAlt);

		txtServicoAlt = new JTextField();
		txtServicoAlt.setBounds(118, 61, 287, 28);
		panelAlterar.add(txtServicoAlt);
		txtServicoAlt.setColumns(10);

		btnServicoAlt = new JButton("Servi\u00E7os");
		btnServicoAlt.setBounds(428, 62, 89, 25);
		panelAlterar.add(btnServicoAlt);
		btnServicoAlt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				FrmEscolheServico escolheServico = new FrmEscolheServico();
				escolheServico.setVisible(true);
				escolheServico.setRetorno(new IRetornaServico() {

					@Override
					public void retorna(Servico servico) {

						serv = servico;
						txtServicoAlt.setText(serv.getNome());
					}
				});

			}
		});

		lblFuncionarioAlt = new JLabel("Funcion\u00E1rio:");
		lblFuncionarioAlt.setFont(new Font("Arial", Font.PLAIN, 16));
		lblFuncionarioAlt.setBounds(10, 114, 96, 14);
		panelAlterar.add(lblFuncionarioAlt);

		cbFuncionarioAlt = new JComboBox();
		cbFuncionarioAlt.setBounds(118, 108, 399, 28);
		panelAlterar.add(cbFuncionarioAlt);

		lblClienteAlt = new JLabel("Cliente:");
		lblClienteAlt.setFont(new Font("Arial", Font.PLAIN, 16));
		lblClienteAlt.setBounds(10, 161, 89, 14);
		panelAlterar.add(lblClienteAlt);

		cbClienteAlt = new JComboBox();
		cbClienteAlt.setBounds(118, 155, 399, 28);
		panelAlterar.add(cbClienteAlt);

		btnSalvarAlt = new JButton("Salvar");
		btnSalvarAlt.setBounds(228, 206, 89, 23);
		panelAlterar.add(btnSalvarAlt);
		btnSalvarAlt.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				tabbedAgendamento.setEnabledAt(1, false);
				tabbedAgendamento.setEnabledAt(0, true);
				tabbedAgendamento.setSelectedIndex(0);
				alt();
				enableCampos();
				
			}
		});

		btnCancelarAlt = new JButton("Cancelar");
		btnCancelarAlt.setBounds(335, 206, 89, 23);
		panelAlterar.add(btnCancelarAlt);
		btnCancelarAlt.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				tabbedAgendamento.setEnabledAt(1, false);
				tabbedAgendamento.setEnabledAt(0, true);
				tabbedAgendamento.setSelectedIndex(0);
				enableCampos();
				
			}
		});

		btnAlterar = new JButton("Alterar");
		btnAlterar.setBounds(50, 505, 141, 38);
		contentPane.add(btnAlterar);
		btnAlterar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				tabbedAgendamento.setEnabledAt(1, true);
				tabbedAgendamento.setEnabledAt(0, false);
				tabbedAgendamento.setSelectedIndex(1);
				disableCampos();
				ctrl.carregaAgendamento(dtchDataAgendamento, cbFuncionario, cbListaClientes, cbHorarioAlt, serv, tbAgendamento, txtServicoAlt, cbFuncionarioAlt, cbClienteAlt, dtDataAlt);
				
			}
		});

		btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(436, 505, 141, 38);
		contentPane.add(btnCancelar);
		btnCancelar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				
			}
		});

		btnExcluir = new JButton("Excluir");
		btnExcluir.setBounds(243, 505, 141, 38);
		contentPane.add(btnExcluir);
		btnExcluir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				remove();
				
			}
		});

		this.addWindowListener(new WindowAdapter() {
			public void windowOpened(WindowEvent e) {
				load();
			}
		});

	}

	public void load() {
		ctrl.preencheCbCliente(cbListaClientes);
		ctrl.preencheCbFuncionario(cbFuncionario);
	}
	
	public void validaHorario(){
		cbHorarioAlt.removeAll();
		ctrl.preencheCbHorario(cbHorarioAlt, dtchDataAgendamento);
		ctrl.preencheTabela(tbAgendamento, dtchDataAgendamento);
	}
	
	public void alt(){
		ctrl.altera(tbAgendamento, dtDataAlt, cbFuncionarioAlt, cbClienteAlt, cbHorarioAlt, serv, cbListaClientes, cbFuncionario, dtchDataAgendamento);
	}
	
	
	public void remove(){
		ctrl.remove(cbFuncionario, cbListaClientes, dtchDataAgendamento, tbAgendamento);
	}
	
	public void clean(){
		dtchDataAgendamento.setDate(null);
		cbHorarioAlt.removeAllItems();
		serv = null;
		txtServicoAlt.setText("");
		
		
	}
	
	public void enableCampos(){
		cbFuncionario.setEnabled(true);
		cbListaClientes.setEnabled(true);
		dtchDataAgendamento.setEnabled(true);
		btnSearch.setEnabled(true);
	}
	
	public void disableCampos(){
		cbFuncionario.setEnabled(false);
		cbListaClientes.setEnabled(false);
		dtchDataAgendamento.setEnabled(false);
		btnSearch.setEnabled(false);
	}
}
